#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class Queue {
public:
    Queue(int *a)
    : arr(a) {}
    ~Queue() { delete [] arr;}
    void push(int i) {
        while (!q.empty() && arr[q.back()] > arr[i]) {
            q.pop_back();
        }
        q.push_back(i);
    }
    void pop() { ++head; }
    int size() { return q.size(); }
private:
    int head, tail;
    int *arr;
    deque<int> q;
};

int main() {
    int n;
    cin >> n;
    int *a = new int[n + 5], *b = new int[n + 5], ans;
    Queue q1(a), q2(b);
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    for (ans = 0; ans < n; ++ans) {
        q1.push(ans);
        q2.push(ans);
        if (q1.size() != q2.size()) break;
    }
    cout << ans << endl;
    return 0;
}